function switch_tab(t){
	if(t=='t_0'){
		$("#t_0").addClass("on");
		$("#t_1").removeClass("on");
		$("#wt_0").show()
		$("#wt_1").hide()
	} else {
		$("#t_1").addClass("on");
		$("#t_0").removeClass("on");
		$("#wt_1").show()
		$("#wt_0").hide()
	}
}

function viewport_size() {
    var b = [0, 0];
    if (typeof window.innerWidth != "undefined") {
        b = [window.innerWidth, window.innerHeight]
    } else {
        if (typeof
        document.documentElement != "undefined" && typeof
        document.documentElement.clientWidth != "undefined" && document.documentElement.clientWidth != 0) {
            b = [document.documentElement.clientWidth, document.documentElement.clientHeight]
        } else {
            b = [document.body.clientWidth, document.body.clientHeight]
        }
    }
    return b
}

function display_limit(c,b, h) {
    c.keyup(function(){
	var
    g,
    d = function(i) {
        var
        f = c.val();
        if (f == g) {
            return
        }
        if (f.length >= b) {
            c.val(f.substring(0, b))
        }
        h.text(b - c.val().length);
        g = c.val()
    };
    d()
	})
}

function init_star(r,si,w) {
    var b = {
        1: "很差",
        2: "较差",
        3: "还行",
        4: "推荐",
        5: "力荐"
    },
    h = $(r),
    c = $(si),
    d = function(f) {
        var j = h.val() || 0;
        c.each(function(k) {
            var i = this.src.replace(/\w*\.gif$/, ((k < f) ? "sth": ((k < j) ? "st": "nst")) + ".gif");
			this.src = i
        });
        if (f && w) {
            $("#rateword").text(b[f])
        } else {
            $("#rateword").text(j ? b[j] : "")
        }
    };
    c.hover(function() {
        d(this.id.charAt(4))
    },
    function() {
        d(0)
    });
    if (h.attr("name")) {
        c.click(function() {
            var f = this.id.charAt(4);
            h.val(f);
            d(f);
			
        })
    }
    d();
}


function tag_btn(t,clazz){
	var f={},c={}
	var b = function(i) {
        if (f[i]) {
            c[i] = true;
            $.each(f[i],
            function(k, j) {
                $(j).removeClass("gract").addClass("rdact")
            })
        }
    };
    var d = function(i) {
        if (f[i]) {
            delete
            c[i];
            $.each(f[i],
            function(k, j) {
                $(j).removeClass("rdact").addClass("gract")
            })
        }
    };

	$(clazz).each(function(k) {
        var j = $(this).text().toLowerCase();
        if (f[j]) {
            f[j].push(this)
        } else {
            f[j] = [this]
        }
    }).click(function() {
		var j = $(this).text();
        var l = $.trim($(t)[0].value).split(" "),
        p = false,
        k = j.toLowerCase(),
        n;
		l = $.grep(l,
        function(r, q) {
            if (r.toLowerCase() == k) {
                d(k);
                p = true;
                return false
            } else {
                return true
            }
        });
        if (!p) {
            l.push(j);
            b(k)
        }
        var
        o = l.join(" ");
        $(t)[0].value = (o.length > 1) ? o.trim() + " ": o;
        $(t)[0].focus()
    });
}

function collect_btn(b) {
        if ($("#hiddendialog").length) {
            show_dialog($("#hiddendialog").html());
            //load_event_monitor($("#dialog"))
        } else {
            show_dialog(null);
            var
            f = b.attr("name").split("-"),
            h = f[0],
            c = f[1],
            i = f[2],
            g = f[3],
            d = "/dialog/"+c+"?t=1" + (i ? "&interest=" + i: "") + (g ? "&rating=" + g: "&rating=0") + (h == "cbtn" ? "&cmt=1": "");
			$.post(d,
            function(j) {               
								if(j=='login_required'){
										$("#overlay").hide();
										$("#dialog").hide();
									  location.href = "/login?redirect="+location.href
								}
                if ($("#dialog").length) {
                    var n = $("<div></div>").html(j);
                    if (h == "pbtn" || h == "cbtn") {
                        $("form", n).data("reload", 1)
                    }
                    $("#dialog").html(n);
                    $("#showtags").click(function() {
                        if ($("#advtags").is(":hidden")) {
                            $(this).html("缩起▲");
                            $("#advtags").show();
                            $("#foldcollect").val("U")
                        } else {
                            $(this).html($(this).attr("rel"));
                            $("#advtags").hide();
                            $("#foldcollect").val("F")
                        }
                        $(this).blur();
                        refine_dialog()
                    });

					init_star("#n_rating","#stars img");
					tag_btn('#input_tags','.tagbtn1');
					tag_btn('#input_impression','.tagbtn2');                  
					$('#interest_form',n).submit(function() {
				        remote_submit(this,
				        function(j) {
							location.href = location.href
				        },
				        false);
				        $("#submits").hide().after('<span class="m" id="saving">正在保存...</span>');
				        refine_dialog();
				        return false
				    });
                    var k = $("input[type='radio']"),
                    q = $(".rate_stars"),
                    o = function() {
                        if (k[0].checked) {
                            q.hide()
                        } else {   
                            q.show()
                        }
                        refine_dialog()
                    };
                    k.click(o);
                    o();
                    if ($("#left_n").length) {
                        display_limit($("#comment"),140, $("#left_n"))
                    }
                    if (h == "cbtn") {
                        var
                        s = $("h2", "#dialog");
                        s.text(s.text().replace("修改", "写短评"));
                        if (!k[0].checked && k[1]) {
                            k[1].checked = true
                        }
                        $("form", "#dialog").data("comment", 1)
                    }
                    //load_event_monitor(n)
            	}
            })
        }

}

function show_dialog(c, b) {
    if ($("#dialog").length) {
        return
    }
    $("body").prepend('<div id="overlay"></div><div id="dialog" style="width:' + (b || 550) + 'px;"></div>');
    if (c != null) {
        $("#dialog").html(c)
    } else {
        $("#dialog").html("<div class='loadpop'>正在载入，请稍候...<span class='gact rr'><a href='javascript:void(O)' onclick='close_dialog()'>x</a></span></div>")
    }
    set_overlay()
}
function set_overlay() {
    var
    d = ($.browser.msie ? 11: 26),
    c = $("#dialog")[0],
    b = c.offsetWidth,
    f = (document.body.offsetWidth - b) / 2 + "px";
    $("#overlay").css({
        height: c.offsetHeight + d,
        width: b + 26,
        left: f
    });
    c.style.left = f
};
function close_dialog() {
    $("#overlay").unbind("click");
    $("#dialog,#overlay,.bgi").remove();
    if (typeof
    document.body.style.maxHeight == "undefined") {
        $("body", "html").css({
            height: "auto",
            width: "auto"
        });
        $("html").css("overflow", "")
    }
    document.onkeydown = "";
    return false
};
function refine_dialog() {
    if (!$("#dialog").length) {
        return
    }
	var c = 0.5 * (viewport_size[1] - $("#dialog")[0].offsetHeight) + 140;
	$("#dialog,#overlay").css("top", c);
    set_overlay()
};

function open_dialog(b,t){
	if ($("#hiddendialog").length) {
        show_dialog($("#hiddendialog").html());
        //load_event_monitor($("#dialog"))
    } else {
        show_dialog(null);
   		var f = b.attr("name").split("-"),
	    h = f[0],
	    c = f[1],
	    d = "/dialog/"+c+"?t="+t;
		$.post(d,
        function(j) {
			
            if ($("#dialog").length) {
                var n = $("<div></div>").html(j);
                if (h == "pbtn" || h == "cbtn") {
                    $("form", n).data("reload", 1)
                }
                $("#dialog").html(n);
				$('#interest_form').submit(function() {
			        remote_submit(this,
			        function(j) {
						location.href = location.href.split("#")[0]
			        },
			        false);
			        $("#submits").hide().after('<div class="m recsubmit" id="saving">正在保存...</div>');
			        refine_dialog();
			        return false
			    });
				refine_dialog()
			}
		})//post
	} 
}

function show_des(){
	if($('#baoliao').text()==''){
		$('li[ref=baoliao]').removeClass().hide();
		$('#baoliao').hide();
		$('#biaoqian').show();
		$('li[ref=biaoqian]').addClass('select')
	}
	if($('#biaoqian span').text().trim()==":"){
		$('li[ref=biaoqian]').removeClass().hide();
		$('#biaoqian').hide();
		$('#chaoxun').show();
		$('li[ref=chaoxun]').addClass('select')
	}
	if($('#chaoxun span').text().trim()==":"){
		$('#tabs').hide();
		$('#baoliao').hide();
		$('#biaoqian').hide();
		$('#chaoxun').hide();
	}
}


function get_form_fields(b) {
    var c = {};
    $(":input", b).each(function(f) {
        var
        d = this.name;
        if (this.type == "radio") {
            if (this.checked) {
                c[d] = this.value
            }
        } else {
            if (this.type == "checkbox") {
                if (this.checked) {
                    c[d] = this.value
                }
            } else {
                if (this.type == "submit") {
                    if (/selected/.test(this.className)) {
                        c[d] = this.value
                    }
                } else {
                    if (d) {
                        c[d] = this.value
                    }
                }
            }
        }
        if (/notnull/.test(this.className) && this.value == "") {
            $(this).prev().addClass("errnotnull");
            c.err = "notnull"
        }
    });
    return c

}
function post_withck(b, d, f, c) {
$.ajax({
        type: "POST",
        url: b,
        data: d,
        success: f,
        dataType: c || "text"
    })
}

function remote_submit (g, f, c, h) {
    var d = get_form_fields(g);
    $(":submit,:input", g).attr("disabled", c == false ? 0: 1);
    var
    b = h || g.action;
    post_withck(b, d,
    function(i) {
        f(i)
    },
    "text")
}
function init_vote(){
 	$("a.report_1").click(function(){
		 var d = this.id.split('_'),
		i=d[0],
		v=d[1],
		ths = this,
		c='/vote/'+i+'/'+v;
		
		$.post(c,function(j){
			if(j=='succ'){
				var n1 = parseInt($(ths).find("label").text())
				$(ths).find("label").text((n1+1))
			} else if(j=='rept'){
				alert('您已经投过票了..')
			}
		})
	 });
      
	$("a.report_2").click(function(){
		 var d = this.id.split('_'),
		i=d[0],
		v=d[1],
		ths = this,
		c='/vote/'+i+'/'+v;

		$.post(c,function(j){
			if(j=='succ'){
				var n2 = parseInt($(ths).find("label").text())
				$(ths).find("label").text((n2+1))
			} else if(j=='rept'){
				alert('您已经投过票了..')
			}
		})
	 });
}

$(function() {
    $('#tabs li').mouseover(function() {
        $('#tabs li').removeClass()
        $(this).addClass('select')
        $('.info_detail_tab').hide()
        $('#' + $(this).attr("ref")).show()
    });
    

    $('#wish').click(function(e) {
		collect_btn($(this))
        return false;
    });
    $('#collect').click(function(e) {
		collect_btn($(this))
		return false;
    });
		
	$('#recommendbtn').click(function(){
		open_dialog($(this),2);
		return false;
	})
	
	$('#collectbtn').click(function(){
		open_dialog($(this),3);
		return false;
	})

	$('.a_collect_btn').click(function(){
		collect_btn($(this))
		return false;
	})
    	
	init_star("#n_rating","#stars img",true);
	init_star("#n_general","#general img",false);
	init_star("#n_face","#face img",false);
	init_star("#n_confort","#confort img",false);
  tag_btn('#tag_input','.tagbtn0');
	show_des();
	init_vote();
	
	
	var k = $("input[name='interest']")
	k.click(function(){
		if(this.value=='wish'){
			$('#ratings').hide()
		} else {
			$('#ratings').show()
		}
	})
});


